﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Model
{
    public class MaterialSimpleOB
    {
        public string MaterialCode { get; set; }
        public string MaterialName { get; set; }
        public string MaterialUnit { get; set; }
        //public string FullDisplayName { get { return string.Format("{0:10}", MaterialCode + "|" + MaterialName); } }
        public static IList<MaterialSimpleOB> GetMaterialSimpleList()
        {

            IList< MaterialSimpleOB> list = new List< MaterialSimpleOB>();
            try
            {
                string sql = "SELECT ItemCode,ItemName, InvntryUom FROM OITM";

                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                IDataReader reader = database.ExecuteReader(dbCommand);
                while (reader.Read())
                {
                     MaterialSimpleOB obj = new MaterialSimpleOB();
                    obj.MaterialCode = reader["ItemCode"] as string;
                    obj.MaterialName = reader["ItemName"] as string;
                    obj.MaterialUnit = reader["InvntryUom"] as string;
                    list.Add(obj);
                }
                reader.Close();
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static GridPageEntity<MaterialSimpleOB> GetMaterialSimplePaged(int pageno, int pagesize)
        {
            GridPageEntity<DuJianliang.ApplicationPlatform.Model.MaterialSimpleOB> result = new GridPageEntity<DuJianliang.ApplicationPlatform.Model.MaterialSimpleOB>();
            result.PageNo = pageno;
            result.PageSize = pagesize;
            IList<DuJianliang.ApplicationPlatform.Model.MaterialSimpleOB> list = new List<DuJianliang.ApplicationPlatform.Model.MaterialSimpleOB>();
            try
            {
                string sql = string.Format("Select * From (Select (ROW_NUMBER() OVER(ORDER By ItemCode asc)) AS rownum,ItemCode,ItemName, InvntryUom From OITM) t Where rownum BETWEEN {0} AND {1}", (pageno - 1) * pagesize + 1, pageno * pagesize);

                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                IDataReader reader = database.ExecuteReader(dbCommand);
                while (reader.Read())
                {
                    DuJianliang.ApplicationPlatform.Model.MaterialSimpleOB obj = new Model.MaterialSimpleOB();
                    obj.MaterialCode = reader["ItemCode"] as string;
                    obj.MaterialName = reader["ItemName"] as string;
                    obj.MaterialUnit = reader["InvntryUom"] as string;
                    list.Add(obj);
                }
                reader.Close();
                result.Rows = list;

                dbCommand = database.GetSqlStringCommand("SELECT Count(ItemCode) FROM OITM");
                reader = database.ExecuteReader(dbCommand);
                if (reader.Read())
                {
                    int rowcount = reader.GetInt32(0);
                    result.PageCount = (rowcount % pagesize == 0) ? rowcount / pagesize : rowcount / pagesize + 1;
                } reader.Close();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static  MaterialSimpleOB GetMaterialSimpleOBByCode(string itemcode)
        { 
            
            try
            {
                string sql = "SELECT ItemCode,ItemName, InvntryUom FROM OITM where ItemCode =@ItemCode ";
                MaterialSimpleOB obj =null ;
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, itemcode); 
                IDataReader reader = database.ExecuteReader(dbCommand);
                if (reader.Read())
                {
                    obj = new MaterialSimpleOB();
                    obj.MaterialCode = reader["ItemCode"] as string;
                    obj.MaterialName = reader["ItemName"] as string;
                    obj.MaterialUnit = reader["InvntryUom"] as string;
                   
                }
                reader.Close();
                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}